﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WenslyWuLib.Common
{
    public class DelegateCommand : ICommand
    {
        public Action<object> ExecuteAction { get; private set; }

        public Func<object, bool> CanExecuteFunc { get; private set; }


        public DelegateCommand(Action<object> execute, Func<object, bool> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute can not be null!");
            }
            this.ExecuteAction = execute;
            this.CanExecuteFunc = canExecute;
        }


        public bool CanExecute(object parameter)
        {
            if (this.CanExecuteFunc != null)
            {
                return this.CanExecuteFunc(parameter);
            }
            else
            {
                return true;
            }
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (this.CanExecute(parameter))
            {
                this.ExecuteAction(parameter);
            }
        }
    }
}
